interface IReportBasicData {
  poolNo: string;
  date: number;
  reporter: string;
}
interface IFeedLossData extends IReportBasicData {
  feed: number;
  loss: number;
  lossWeight: number;
};
interface IDivideData {
  poolNoOut: string;
  poolNoIn: string;
  date: number;
  reporter: string;
  quantity: number;
  weight: number;
};
interface ISaleData extends IReportBasicData {
  // 0普1A
  saleType: 0 | 1;
  quantity: number;
  weight: number;
};
interface IPutData extends IReportBasicData {
  quantity: number;
  weight: number;
  isUpdate: number;
};
interface IGetParams {
  date: string[]
}
function basicRequest(url: string, data?: any) {
  const method = url === '/site' ? 'GET' : 'POST';
  const token = wx.getStorageSync('token');
  return new Promise((resolve, reject) => {
    wx.request({
      url: `https://www.taishangongrong.com:88${url}`,
      method,
      data,
      header: {
        'content-type': 'application/json', // 默认值
        'Cookie': `token=${token}`
      },
      success(res) {
        // 响应拦截
        if (res.statusCode === 201) {
          reject('登录过期');
          wx.navigateTo({
            url: '../login/login'
          })
        } else resolve(res.data)
      },
      fail(err) {
        reject(err)
      }
    })
  })
}
function reportFeedLoss(data: IFeedLossData) {
  return basicRequest('/feedLoss/report', data);
};
function reportDivide(data: IDivideData) {
  return basicRequest('/divide/report', data);
}
function reportSale(data: ISaleData) {
  return basicRequest('/sale/report', data);
}
function reportClearLoss(data: IReportBasicData) {
  return basicRequest('/clearLoss/report', data);
}
function reportPut(data: IPutData) {
  return basicRequest('/put/report', data);
}
function getFeedLoss(data: IGetParams) {
  return basicRequest('/feedLoss/record', data);
};
function getDivide(data: IGetParams) {
  return basicRequest('/divide/record', data);
}
function getSale(data: IGetParams) {
  return basicRequest('/sale/record', { ...data, saleType: 0 });
}
function getSaleA(data: IGetParams) {
  return basicRequest('/sale/record', { ...data, saleType: 1 });
}
function getClearLoss(data: IGetParams) {
  return basicRequest('/clearLoss/record', data);
}
function getPut(data: IGetParams) {
  return basicRequest('/put/record', data);
}
function login(data: { userNo: string, password: string }) {
  const token = wx.getStorageSync('token');
  return new Promise((resolve, reject) => {
    wx.request({
      url: `https://www.taishangongrong.com:88/user/login/1`,
      method: 'POST',
      data,
      header: {
        'content-type': 'application/json', // 默认值
        'Cookie': `token=${token}`
      },
      success(res) {
        resolve(res.data);
      },
      fail(err) {
        reject(err)
      }
    })
  })
}
function logout() {
  return new Promise((resolve, reject) => {
    wx.request({
      url: `https://www.taishangongrong.com:88/user/logout`,
      method: 'POST',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        resolve(res.data)
      },
      fail(err) {
        reject(err)
      }
    })
  })
}
function getSite() {
  return basicRequest('/site');
}
function updatePassword(data: { userNo: string, password: string, oldPassword: string }) {
  return basicRequest('/user/updatePassword', data);
}
const api = {
  reportFeedLoss,
  reportDivide,
  reportSale,
  reportClearLoss,
  reportPut,
  getFeedLoss,
  getDivide,
  getSale,
  getSaleA,
  getPut,
  getClearLoss,
  login,
  logout,
  getSite,
  updatePassword
}
export default api;